package com.example.usbdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {

    private int count=0;
    private UsbManager usbManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
//        IntentFilter intentFilter = new IntentFilter();
//        intentFilter.addAction("android.hardware.usb.action.USB_STATE");
//        registerReceiver(new USBReceiver(),intentFilter);
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.android.systemui","com.android.systemui.usb.UsbStorageActivity"));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    private class USBReceiver extends BroadcastReceiver{

        AlertDialog alertDialog;
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(action.equals("android.hardware.usb.action.USB_STATE")){
                if (intent.getExtras().getBoolean("connected")){
                    if(count==0){
                        // usb 插入
//                        usbManager.setCurrentFunction(UsbManager.USB_FUNCTION_MTP);
//                        usbManager.setUsbDataUnlocked(true);
                        if(alertDialog==null){
                            AlertDialog.Builder builder = new AlertDialog.Builder(context);
                            String items[] = {"仅充电","传输文件"};
                            builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Log.e("androidLog","which="+which);
                                    if(which==0){
                                        setCurrentFunction(usbManager,"adb");
                                        setUsbDataUnlocked(usbManager,true);
//                                        usbManager.setCurrentFunction(UsbManager.USB_FUNCTION_ADB);
//                                        usbManager.setUsbDataUnlocked(true);
                                        alertDialog.dismiss();
                                    }else if(which==1){
                                        setCurrentFunction(usbManager,"mtp");
                                        setUsbDataUnlocked(usbManager,true);
//                                        usbManager.setCurrentFunction(UsbManager.USB_FUNCTION_MTP);
//                                        usbManager.setUsbDataUnlocked(true);
                                        alertDialog.dismiss();
                                    }
                                }
                            });
                            builder.setNegativeButton("取消",null);
                            alertDialog = builder.create();
                        }
                        alertDialog.show();
                        count++;
                    }
                }else{
                    //   usb 拔出
                    alertDialog.dismiss();
                    alertDialog = null;
                    count = 0;
                }
            }
        }
    }

    public void setUsbDataUnlocked(UsbManager usbManager,Boolean unLocked){

        try {
            Class cls = UsbManager.class;
            Method method = cls.getMethod("setUsbDataUnlocked",boolean.class);
            method.setAccessible(true);
            method.invoke(usbManager,unLocked);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public void setCurrentFunction(UsbManager usbManager,String usbFunction){

        try {
            Class cls = UsbManager.class;
            Method method = cls.getMethod("setCurrentFunction",String.class);
            method.setAccessible(true);
            method.invoke(usbManager,usbFunction);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }


}
